﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace PlaystationNetworkAPI.Core.US
{
    internal class Collector : Common.Collector
    {
        string _psnId;
        string _PingPageUrl = Properties.Settings.Default.US_Collector_PingPageUrl;
        string _TrophiesUrl = Properties.Settings.Default.US_Collector_TrophiesUrl;
        string _TrophiesHtml;
        string _GamesUrl = Properties.Settings.Default.US_Collector_GamesUrl;
        string _GamesHtml;

        /// <summary>
        /// US Playstation Network Trophies page source code.
        /// </summary>
        internal string TrophiesHtml
        {
            get { return _TrophiesHtml; }
        }

        /// <summary>
        /// US Playstation Network Games page source code.
        /// </summary>
        internal string GamesHtml
        {
            get { return _GamesHtml; }
        }

        /// <summary>
        /// US Playstation Network Collector constructor
        /// </summary>
        /// <param name="psnId">Playstation Network Id to get the information collected.</param>
        internal Collector(string psnId)
            : base()
        {
            _psnId = psnId;
            SetUrls();
            PingPageToGetCookies();
            GetSources();
        }

        /// <summary>
        /// Configure all the URLs to use the current Playstation Network ID
        /// </summary>
        private void SetUrls()
        {
            _PingPageUrl = string.Format(_PingPageUrl, _psnId);
            _TrophiesUrl = string.Format(_TrophiesUrl, _psnId);
            _GamesUrl = string.Format(_GamesUrl, _psnId);
        }

        /// <summary>
        /// Added this ping to a the Playstation Network public profile page to get the cookies used on the rest of the section.
        /// </summary>
        private void PingPageToGetCookies()
        {
            using (System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)CreateHttpWebRequest(_PingPageUrl).GetResponse())
            {
                httpWebResponse.Close();
            }
        }

        /// <summary>
        /// Get the source code of the pages.
        /// </summary>
        private void GetSources()
        {
            GetTrophies();
            GetGames();
        }

        /// <summary>
        /// Create a HttpWebRequest object and setup to work with the US Playstation Netowrk web servers.
        /// </summary>
        /// <param name="url">URL that will be used on the request.</param>
        /// <returns>HttpWebRequest setup to work on the US Playstation Netowrk web servers.</returns>
        internal override System.Net.HttpWebRequest CreateHttpWebRequest(string url)
        {
            System.Net.HttpWebRequest httpWebRequest = base.CreateHttpWebRequest(url);

            httpWebRequest.Referer = Properties.Settings.Default.US_Collector_Request_Referer;

            return httpWebRequest;
        }

        /// <summary>
        /// Get the torphies page source code.
        /// </summary>
        private void GetTrophies()
        {
            _TrophiesHtml = GetSourceCode(_TrophiesUrl);
        }

        /// <summary>
        /// Get the games page source code.
        /// </summary>
        private void GetGames()
        {
            _GamesHtml = GetSourceCode(_GamesUrl);
        }
    }
}
